package com.limao.CacheManager;

import java.util.List;

import com.Hazelcast.Cache;
import com.Hazelcast.CacheFactory;
import com.limao.DBDataClass.SysLevelDataClass;
import com.limao.DBProvider.SysLevelProvider;
import com.limao.Tool.CacheClass;
import com.limao.cache.BasicModule;

public class SyslevelManager  extends BasicModule{

	private SysLevelProvider provider;
	private static SyslevelManager instance;
    
    public static SyslevelManager getInstance() {
        return instance;
    }

    private Cache<String, SysLevelDataClass> Sys_levelManager;


    public SyslevelManager() {
    	super("SyslevelManager");
    	Sys_levelManager = CacheFactory.createCache("cd_SyslevelManager");
		initProvider();

    }
    
    @Override
	public void initialize() {
        super.initialize();
        instance=this;
    }
    
    public void ClearObjectCache()
    {
		if(Sys_levelManager.size()>0)
		{
			Sys_levelManager.clear();
		}

    } 
    
	
	private void loadSysLevel()
	{
		if(Sys_levelManager.size()<=0)
		{
			List<SysLevelDataClass> list=provider.loadSysLevel();
			for(SysLevelDataClass obj:list)
			{
				Sys_levelManager.put(obj.level, obj);
			}
		}
	}
	

	
	public int loaduserLevelParam(float nowscore)
	{
		int level=0;
		if(Sys_levelManager.size()<=0)
			loadSysLevel();
		for (String key : Sys_levelManager.keySet()) 
		{
			SysLevelDataClass obj=Sys_levelManager.get(key);
			if(nowscore>=CacheClass.getFloatValue(obj.minvalue) && nowscore<CacheClass.getFloatValue(obj.maxvalue))
			{
				level=CacheClass.getIntValue(obj.level);
				break;
			}
		}
		return level;
	}
	
	
	public SysLevelDataClass loadSysLevel(String level)
	{
		SysLevelDataClass obj=Sys_levelManager.get(level);
		if(obj==null)
		{
			loadSysLevel();
			obj=Sys_levelManager.get(level);
		}
		return obj;
	}	
	
	
	private void initProvider() {
		if (provider == null) {
			try {
				provider = SysLevelProvider.getInstance();
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}
	

	
}
